package com.casino.tool;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

import com.bj.MathUtil;

public class CSM {
	private List<P> pList = new ArrayList<P>();

	private Queue<P> bufferPList;

	private List<P> rList = new ArrayList<P>();
	
	public CSM(int deckQty, int bufferSize) {
		for (int i = 0; i < deckQty; i++) {
			pList.addAll((new Deck()).getpList());
		}
		bufferPList = new ArrayBlockingQueue<P>(bufferSize + 1);
		for (int i = 0; i < bufferSize; i++) {
			int n1 = MathUtil.randomNum(pList.size());
			P p1 = (P) pList.get(n1);
			pList.remove(n1);
			bufferPList.add(p1);
		}
	}

	public P dealRandomCard() {
		int n1 = MathUtil.randomNum(pList.size());
		
		P p1 = (P) pList.get(n1);
		pList.remove(n1);
		bufferPList.add(p1);
		P dealP =bufferPList.poll();
		rList.add(dealP);
		return dealP;
	}
	
	
	
	
	public P dealSpeicalCard(int num) {
		P rm = null;
		for (P p : pList) {
			if ( p.getNum() == num) {
				rm = p;
				break;
			}
		}
		this.rList.add(rm);
		if (rm != null) {
			pList.remove(rm);
			return rm;
		}
		
		return rm;
	}

	

	public void returnCardsToMachine() {
		this.hiLoCount = 0;
		this.realCount = 0;

		for (int i = 0; i < rList.size(); i++) {
			bjCount(rList.get(i));
		}
		this.pList.addAll(rList);
		rList.clear();
	}

	private double hiLoCount = 0;
	private double realCount = 0;
	private double tenCount = 0;

	public double getHiLoCount() {
		return hiLoCount;
	}

	public double getRealCount() {
		return realCount;
	}

	private void bjCount(P p1) {
		if (p1.getNum() == 2 || p1.getNum() == 3 || p1.getNum() == 4 || p1.getNum() == 6 || p1.getNum() == 5) {
			hiLoCount++;
			tenCount++;
		} else if (p1.getNum() == 14 || p1.getNum() == 13 || p1.getNum() == 12 || p1.getNum() == 11 || p1.getNum() == 10) {
			hiLoCount--;
			if (p1.getNum() == 14) {
				tenCount++;
			} else {
				tenCount = tenCount - 2.25;
			}
		} else {
			tenCount++;
		}

		// if (p1.getNum() == 2) {
		// realCount = realCount + 0.7;
		// } else if (p1.getNum() == 3) {
		// realCount = realCount + 0.8;
		// } else if (p1.getNum() == 4) {
		// realCount = realCount + 1.1;
		// } else if (p1.getNum() == 5) {
		// realCount = realCount + 1.5;
		// } else if (p1.getNum() == 6) {
		// realCount = realCount + 1.4;
		// } else if (p1.getNum() == 7) {
		// realCount = realCount + 0.4;
		// } else if (p1.getNum() == 8) {
		// realCount = realCount - 0.1;
		// } else if (p1.getNum() == 9) {
		// realCount = realCount - 0.4;
		// } else if (p1.getNum() >= 10) {
		// realCount = realCount - 0.9;
		// }
		realCount = (this.hiLoCount / (pList.size() / 52));
	}
}
